home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 10 Scripting / 01 Berger / CodeGen.H < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-10  |  1.5 KB  |  50 lines

  1. #ifndef __CodeGen_H__
  2. #define __CodeGen_H__
  3.  
  4. #include <stdio.h>
  5.  
  6. #include "Opcode.H"
  7. #include "PTNode.H"
  8.  
  9.  
  10. // This is the code generator class.  Once the parser has completed building
  11. // the parse tree, it passes the tree into this class to generate the bytecode
  12. // stream.  This class simply needs to iterate through the nodes in the parse
  13. // tree and generate all the proper instructions.
  14. class CodeGen {
  15. public:
  16.   CodeGen();
  17.   ~CodeGen();
  18.  
  19.   // This function is the only public API to this class.  It expects the root
  20.   // of the parse tree that it will generate the bytecode stream from.
  21.   void Gen( PTNodePtr node );
  22.  
  23.  
  24. protected:
  25.   // These series of functions are used internally to generate the proper
  26.   // instructions for the various types of parse tree nodes.  These functions
  27.   // help break up the code generation logic.
  28.   void GenAdd( AddNodePtr add );
  29.   void GenSubtract( SubtractNodePtr subtract );
  30.   void GenMultiply( MultiplyNodePtr multiply );
  31.   void GenDivide( DivideNodePtr divide );
  32.   void GenConstant( ConstantNodePtr constant );
  33.   void GenStatement( StatementNodePtr stmt );
  34.   void GenBlock( BlockNodePtr block );
  35.  
  36.   // These two functions handle writing opcodes and an opcodes argument to the
  37.   // bytecode stream.
  38.   void WriteOpcode( Opcode op );
  39.   void WriteArg( int arg );
  40.  
  41. private:
  42.   // This file handle points to the bytecode stream that the code generator is
  43.   // building.
  44.   FILE *out;
  45. };
  46.  
  47.  
  48.  
  49. #endif // __CodeGen_H__
  50.